<?php
    /*
     * Assetdetails model class to save/retrieve the asset 
     */
class Asset_Model_Assetdetails extends WOW_Model_Acl_Abstract
{
    public function getResourceId()
    {
        return 'Assetdetails';
    }

    public function saveAssetDetails($post,$asset_id)
    {
        return $this->_save($post,$asset_id);
    }

    protected function _save(array $data,$asset_id)
    {
        if (count($data) == 0)
           return false;
        
        foreach($data as $key=>$value)
        {
            $exp = explode('+',$key);
            $newstr = str_replace("%20", " ", $exp[0]);
            $data['assetid'] = $asset_id;
            $data['FormFieldId'] = $exp[1];
            $data['FormFieldName'] = $newstr;
            $data['FormFieldValue'] = $value;
            $data['updatedAt'] = date('Y-m-d H:i:s');
            $res = $this->getResource('Assetdetails')->saveRow($data);
        }
        return $res;
    }
    
    public function getAssetDetails($asset_id)
    {
        $temp_user_assetdetails = $this->getResource('Assetdetails')->getAssetDetailsByAssetId($asset_id);
        $user_assetdetails = $temp_user_assetdetails->toArray();
        return $user_assetdetails;
    }
    
    public function updateAsset(array $post)
    {
        return $this->_update($post);
    }
    
    protected function _update(array $data)
    {
        unset($data['updateBtn']);
        $asset_id = array_pop($data);
        $update_array = array();
        foreach($data as $key=>$val)
        {
            $split_value = explode('___',$key);
            $formfieldid = $split_value[1];
            $update_array['FormFieldValue'] = $val; 
            $res = $this->getResource('Assetdetails')->update(
                    $update_array,
                    array(
                            'assetid = ?' => $asset_id,
                            'FormFieldId = ?' => $formfieldid
                         )
                    );
        }
        return $res;
    }
    
    #@ TODO:: needs to be worked on ACL later
    public function setAcl(WOW_Acl_Interface $acl)
    {
        if (!$acl->has($this->getResourceId()))
        {
            $acl->add($this)->allow('Normal', $this, array('register'))
            ->allow('Moderator', $this, array('saveCategory'))
                    ->allow('Admin', $this);
        }
        $this->_acl = $acl;
        return $this;
    }

    public function getAcl()
    {
        if (null === $this->_acl) {
            $this->setAcl(new Main_Model_Acl_Main());
        }
        return $this->_acl;
    }
}

?>
